package cn.edu.jxau.controller;

import java.io.IOException;
import java.sql.SQLException;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.edu.jxau.domain.PageBean;
import cn.edu.jxau.domain.post;
import cn.edu.jxau.service.queryPost;

public class readPostServlet extends HttpServlet {

	/**
	 * 从数据库中读取帖子 数字1代表美食天下类型的帖子 数字2代表旅游休闲
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");

		String value = request.getParameter("type");
		/**
		 * 1.获取页面传递的pc 2.给定ps的值 3.使用pc和ps调用service的方法得到pageBean，保存到request的域中
		 */
		queryPost queryPost = new queryPost();
		
			int pc = getPc(request);// 得到pc
			int ps = 5;// 给定ps的值，每页10行记录
			PageBean<post> postList;
			try {
				postList = queryPost.findAllByType(pc, ps, value);
				HttpSession session = request.getSession();
				session.setAttribute("postlist", postList);
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			response.sendRedirect("topic.jsp");
		

		

	}

	private int getPc(HttpServletRequest req) {
		/*
		 * 1. 查看是否存在pc参数，如果存在，转换成int类型 2. 如果不存在pc参数，返回1
		 */
		String pc = req.getParameter("pc");
		if (pc != null && !pc.trim().isEmpty()) {
			return Integer.parseInt(pc);
		}
		return 1;
	}

}
